<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #ppxh="{ record }">
        <div> {{ getDictLabel('car_type', record.zcmxCx, record.zcmxCx) || '' }}</div>
      </template>
      <template #toolbar>
        <!--        <a-button type="primary" @click="handleForm({})" v-auth="'cardd:zcmx:carddZcmx:edit'">-->
        <!--          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}-->
        <!--        </a-button>-->
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" />
          {{ t('导出') }}
        </a-button>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsCarddZcmxCarddZcmxList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicColumn, BasicTable, useTable } from '/@/components/Table';
  import { carddZcmxDelete, carddZcmxListData } from '/@/api/cardd/zcmx/carddZcmx';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';

  import { useDict } from '/src/components/Dict';

  const { initDict, getDictLabel } = useDict();

  initDict('car_type');

  const { t } = useI18n('cardd.zcmx.carddZcmx');
  const { showMessage } = useMessage();
  const { meta, query } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('租车明细表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('车单编号'),
        field: 'zcmxCdbh',
        component: 'Input',
      },
      {
        label: t('上车时间'),
        field: 'zcmxScsj',
        component: 'Input',
      },
      {
        label: t('下车时间'),
        field: 'zcmxXcsj',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('车单编号'),
      dataIndex: 'zcmxCdbh',
      key: 'a.zcmx_cdbh',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('上车时间'),
      dataIndex: 'zcmxScsj',
      key: 'a.zcmx_scsj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('下车时间'),
      dataIndex: 'zcmxXcsj',
      key: 'a.zcmx_xcsj',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('用车人'),
      dataIndex: 'zcmxYcr',
      key: 'a.zcmx_ycr',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('车型'),
      dataIndex: 'zcmxCx',
      key: 'a.zcmx_cx',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: 'car_type',
      slot: 'ppxh',
    },
    {
      title: t('车牌号'),
      dataIndex: 'zcmxCph',
      key: 'a.zcmx_cph',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('驾驶员'),
      dataIndex: 'zcmxJsy',
      key: 'a.zcmx_jsy',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('行驶路线'),
      dataIndex: 'zcmxXslx',
      key: 'a.zcmx_xslx',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('服务时长'),
      dataIndex: 'zcmxFwsz',
      key: 'a.zcmx_fwsz',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('实际里程'),
      dataIndex: 'zcmxSjlc',
      key: 'a.zcmx_sjlc',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('租车单价'),
      dataIndex: 'zcmxZcdj',
      key: 'a.zcmx_zcdj',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('租车费'),
      dataIndex: 'zcmxZcf',
      key: 'a.zcmx_zcf',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('超里程数'),
      dataIndex: 'zcmxClcs',
      key: 'a.zcmx_clcs',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('超公里'),
      dataIndex: 'zcmxCgl',
      key: 'a.zcmx_cgl',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('超里程费'),
      dataIndex: 'zcmxClcf',
      key: 'a.zcmx_clcf',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('超时时长'),
      dataIndex: 'zcmxCssz',
      key: 'a.zcmx_cssz',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('超时单价'),
      dataIndex: 'zcmxCsdj',
      key: 'a.zcmx_csdj',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('超时费'),
      dataIndex: 'zcmxCsf',
      key: 'a.zcmx_csf',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('过路费'),
      dataIndex: 'zcmxGlf',
      key: 'a.zcmx_glf',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('洗车费'),
      dataIndex: 'zcmxXcf',
      key: 'a.zcmx_xcf',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('停车费'),
      dataIndex: 'zcmxTcf',
      key: 'a.zcmx_tcf',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('餐费'),
      dataIndex: 'zcmxCf',
      key: 'a.zcmx_cf',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('住宿费'),
      dataIndex: 'zcmxSf',
      key: 'a.zcmx_sf',
      sorter: true,
      width: 130,
      align: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑租车明细表'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'cardd:zcmx:carddZcmx:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: carddZcmxListData,
    beforeFetch: (params) => {
      params.zcmxYcdw = query.zcmxYcdw;
      params.zcmxYf = query.zcmxYf;
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await carddZcmxDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  async function handleExport() {
    const { ctxPath } = useGlobSetting();
    const { zcmxYcdw, zcmxYf } = query;
    downloadByUrl({
      url:
        ctxPath +
        `/ureport/excel?_u=file:单位租车明细.ureport.xml&zcmxYcdw=${zcmxYcdw}&zcmxYf=${zcmxYf}`,
      fileName: `${zcmxYcdw}单位${zcmxYf}租车明细表.xlsx`,
    });
  }
</script>
